<?php

namespace App\Http\Livewire\Archives;

use App\Models\Archive;
use Illuminate\Support\Facades\Gate;
use Livewire\Component;

class Delete extends Component
{
    public $confirmingDelete = false;

    public $archive;

    public $title = '确认删除？';

    public $message = '是否确认删除这条内容';

    public $showDeleteBtn = true;
    public function mount(Archive $archive)
    {
        $this->archive = $archive;
    }
    public function confirmDelete()
    {
        $can = Gate::inspect('delete', $this->archive);
        if (!$can->allowed()) {
            $this->emit('unauthorized');
        } else {
            $this->archive->delete();
            $backTo = $this->archive->category->slug . '.index';
            session()->flash('message', __('Successfully deleted.'));
            return redirect()->route($backTo);
        }
    }

    public function delete()
    {
        $can = Gate::inspect('delete', $this->archive);
        if (!$can->allowed()) {
            $this->showDeleteBtn = false;
            $this->title = __('Unauthorized');
            $this->message = '您无权进行此操作';
        }
        $this->confirmingDelete = true;
    }

    public function render()
    {
        return view('livewire.archives.delete');
    }
}
